package org.example.struct.array2;

import com.alibaba.fastjson.JSON;

import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;

/**
 * @author liyishan
 * @date 2023/10/30 10:11
 * @apiNote
 */

public class Test00950DeckRevealedIncreasing {


    public static void main(String[] args) {
        int[] deck = {17,13,11,2,3,5,7};
        int[] res = test2(deck);
        System.out.println(JSON.toJSON(res));
    }

    public static int[] test2(int[] deck) {
        int n = deck.length;
        Deque<Integer> index = new LinkedList<>();
        Arrays.sort(deck);
        for(int i = n-1;i>=0;i--){
            index.add(deck[i]);
            if(i == 0){
                break;
            }
            index.add(index.poll());
        }
        for(int i = n-1;i>=0;i--){
            deck[i] = index.poll();
        }
        return deck;
    }

    public static int[] test(int[] deck) {
        int n = deck.length;
        Deque<Integer> index = new LinkedList<>();
        for (int i = 0; i < n; i++) {
            index.add(i);
        }
        int[] ans = new int[n];
        Arrays.sort(deck);
        for(int card:deck){
            ans[index.pollFirst()] = card;
            if(!index.isEmpty()){
                index.add(index.pollFirst());
            }
        }
        return ans;
    }
}
